package com.tanhua.server.Controller;

import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.SmallVideosService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService smallVideosService;

    @Autowired
    private CommentService commentService;



    //视频上传
    @PostMapping
    public ResponseEntity smallVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws Exception {
        smallVideosService.saveVideo(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    //小视频列表
    @GetMapping
    public ResponseEntity smallVideos(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult vo = smallVideosService.findVideos(page, pagesize);
        return ResponseEntity.ok(vo);
    }

    //视频点赞
    // POST/smallVideos/:id/like  (视频id)
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String id) {

        Integer count = smallVideosService.like(id);

        return ResponseEntity.ok(count);
    }
     //取消点赞
     @PostMapping("/{id}/dislike")
     public ResponseEntity dislike(@PathVariable("id") String id) {
         //1. 接收参数（动态Id）
         //2. 调用service方法，返回点赞数量
         Integer count = smallVideosService.dislike(id);
         //3. 返回结果
         return ResponseEntity.ok(count);
     }
     //查看视频评论列表
     @GetMapping("/{id}/comments")
     public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @PathVariable("id") String movementId){
         PageResult vo = commentService.findComments(movementId,page,pageSize);
         return ResponseEntity.ok(vo);
     }
     //发布评论
     @PostMapping("/{id}/comments")
     public ResponseEntity comments(@PathVariable("id")String id, @RequestBody  Map map
                                    ){
        String comment = map.get("comment").toString();
         //调用service
        smallVideosService.saveComment(comment,id);
        //返回结果
        return ResponseEntity.ok(null);
    }

    //视频用户关注
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long uid){
        smallVideosService.userFocus(uid);
        return ResponseEntity.ok(null);
    }
    //取消视频用户关注
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long uid){
        smallVideosService.userUnFocus(uid);
        return ResponseEntity.ok(null);
    }
//    //评论点赞
//    @PostMapping("/comments/{id}/like")
//    public ResponseEntity like1(@PathVariable("id") String id) {
//
//
//        Integer count = smallVideosService.like(id);
//
//        return ResponseEntity.ok(count);
//    }




}
